package com.baiku.sanhos.service.impl;

import com.baiku.sanhos.domain.LoginUser;
import com.baiku.sanhos.mapper.MenuMapper;
import com.baiku.sanhos.mapper.UserPerMapper;
import com.baiku.sanhos.mapper.MenuMapper;

import com.baiku.sanhos.mapper.UserPerMapper;
import com.baiku.sanhos.pojo.entity.User;
import com.baiku.sanhos.utils.RedisCache;


import com.baiku.sanhos.utils.RedisCache;
import com.baiku.sanhos.utils.security.CustomUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserPerMapper userPerMapper;

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    RedisCache redisCache;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

       //username必须唯一，一种职业一个帐号一个密码
        // 查询用户信息

        User user = userPerMapper.loadByUsername(username);


        if(Objects.isNull(user)){
            throw  new RuntimeException("用户不存在");
        }

        List<String> list1 = menuMapper.selectPermsByUserId(user.getFlag());
        System.out.println(list1);

        LoginUser user1 = new LoginUser(user,list1);

        return user1;
    }
}
